﻿package net.wangit.adm.user;

import net.wangit.User;
import net.wangit.adm.role.RoleTreeFactory;
import net.wangit.context.LocalContext;
import net.wangit.framework.util.MapListUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.user.AdminUser;
import net.wangit.user.UserFactory;
import net.wangit.user.admin.Admin;
import net.wangit.user.admin.AdminFactory;
import net.wangit.util.ArrayUtil;
import net.wangit.util.Checker;
import net.wangit.view.tree.Tree;

public class UserRoleTreeFactory extends RoleTreeFactory {

	private static final String CHECKBOX_UNCHECKED = "<input type=checkbox disabled/>";
	private static final String CHECKBOX_CHECKED = "<input type=checkbox disabled checked/>";
	private static final String SQL_ROLES = "select roleid from AUSERROLE where userid=? and hold=1";
	private static final String SQL_REPELLENT_ROLES = "select roleid from AUSERROLE where userid=? and hold=0";
	private static final String SQL_INDIRECT_ROLES = "select roleid from AROLE  where  roleid in (select t1.roleid from AORGROLE t1,AUSER t2 where t1.orgid = t2.orgid and t2.userid=?) or roleid in (select t3.roleid from AGROUPROLE t3, AUSERGROUP t4 where t3.groupid=t4.groupid and t4.userid=?)";
	private Admin administrator;
	private String roles[];
	private String repellentRoles[];
	private String indirectRoles[];

	public UserRoleTreeFactory() {
		administrator = null;
	}

	public Tree createTree(String treeDoamin) throws Exception {
		String userId = UserAuthUI.getAndSetUserId();
		if (Checker.isEmpty(userId))
			return null;
		User operator = LocalContext.getLocalContext().getActionContext()
				.getVisitor().getUser();
		if (operator instanceof AdminUser) {
			String administratorId = operator.get("adminid");
			administrator = AdminFactory.getAdmin(administratorId);
		}
		DB db = DBFactory.getDB();
		roles = getRoles(db, userId);
		repellentRoles = getRepellentRoles(db, userId);
		indirectRoles = getIndirectRoles(db, userId);
		User user = UserFactory.getUser(userId);
		String allRoles[] = user.getAllRoles();
		Tree tree = super.createTree(treeDoamin);
		tree.setCheckAttribute("checked disabled");
		tree
				.setAttribute("<input type=checkbox disabled/><input type=checkbox disabled/>");
		tree.setAction((new StringBuilder(" (")).append(roles.length + 1)
				.append("/").append(repellentRoles.length).append("/").append(
						indirectRoles.length).append(") (").append(
						allRoles.length).append("/").append(tree.size())
				.append(")").toString());
		return tree;
	}

	protected void setCustomAttribute(Tree tree, String roleId) {
		tree.setCheckValue(roleId);
		StringBuffer checkbox1 = new StringBuffer();
		if (ArrayUtil.find(roles, roleId) >= 0)
			checkbox1.append("checked");
		StringBuffer checkbox2 = (new StringBuffer(
				"<input type=\"checkbox\" name=\"a.repellentroles\" value=\""))
				.append(roleId).append("\"");
		if (ArrayUtil.find(repellentRoles, roleId) >= 0)
			checkbox2.append(" checked");
		if (administrator != null && !administrator.containsRole(roleId)) {
			checkbox1.append(" disabled");
			checkbox2.append(" disabled");
		} else {
			checkbox1.append(" onclick=exclude(this) ");
			checkbox2.append(" onclick=exclude(this) ");
		}
		tree.setCheckAttribute(checkbox1.toString());
		checkbox2.append("/>");
		if (ArrayUtil.find(indirectRoles, roleId) >= 0)
			tree.setAttribute(checkbox2.append(
					"<input type=checkbox disabled checked/>").toString());
		else
			tree.setAttribute(checkbox2.append(
					"<input type=checkbox disabled/>").toString());
	}

	protected void setRootAttribute(Tree tree1) {
	}

	private static String[] getRoles(DB db, String userId) throws Exception {
		net.wangit.MapList list = db.query(
				"select roleid from AUSERROLE where userid=? and hold=1",
				userId, 2);
		return MapListUtil.getArray(list, "roleid");
	}

	private static String[] getRepellentRoles(DB db, String userId)
			throws Exception {
		net.wangit.MapList list = db.query(
				"select roleid from AUSERROLE where userid=? and hold=0",
				userId, 2);
		return MapListUtil.getArray(list, "roleid");
	}

	private static String[] getIndirectRoles(DB db, String userId)
			throws Exception {
		net.wangit.MapList list = db
				.query(
						"select roleid from AROLE  where  roleid in (select t1.roleid from AORGROLE t1,AUSER t2 where t1.orgid = t2.orgid and t2.userid=?) or roleid in (select t3.roleid from AGROUPROLE t3, AUSERGROUP t4 where t3.groupid=t4.groupid and t4.userid=?)",
						new String[] { userId, userId }, new int[] { 2, 2 });
		return MapListUtil.getArray(list, "roleid");
	}

	protected void setName(Tree tree, String roleId, String roleName,
			int privilegeCount) {
		tree.setName((new StringBuilder(String.valueOf(privilegeCount)))
				.append(" ").append(roleName).toString());
	}
}
